local beiwang = fk.CreateSkill{
  name = "lingling__beiwang",
}

Fk:loadTranslationTable{
  ["lingling__beiwang"] = "北望",
  [":lingling__beiwang"] = "每回合限一次，你可以将一张牌当作上一张被使用的即时牌使用。",

  ["#lingling__beiwang"] = "北望：你可以将一张牌当【%arg】使用",
}

beiwang:addEffect("viewas", {
  anim_type = "offensive",
  pattern = ".",
  prompt = function (self, player, selected_cards, selected)
    return "#lingling__beiwang:::"..player:getMark(beiwang.name)
  end,
  handly_pile = true,
  card_filter = function (self, player, to_select, selected)
    return #selected == 0
  end,
  view_as = function(self, player, cards)
    if #cards ~= 1 then return end
    local card = Fk:cloneCard(player:getMark(beiwang.name))
    card.skillName = beiwang.name
    card:addSubcards(cards)
    return card
  end,
  enabled_at_play = function (self, player)
    return player:getMark(beiwang.name) ~= 0 and player:usedSkillTimes(beiwang.name, Player.HistoryTurn) == 0
  end,
  enabled_at_response = function (self, player, response)
    return not response and player:getMark(beiwang.name) ~= 0 and
      player:usedSkillTimes(beiwang.name, Player.HistoryTurn) == 0 and
      #player:getViewAsCardNames(beiwang.name, {player:getMark(beiwang.name)}) > 0
  end,
  enabled_at_nullification = function (self, player, data)
    return player:getMark(beiwang.name) == "nullification" and
      player:usedSkillTimes(beiwang.name, Player.HistoryTurn) == 0
  end,
})

beiwang:addEffect(fk.AfterCardUseDeclared, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(beiwang.name, true) and
      (data.card.type == Card.TypeBasic or data.card:isCommonTrick())
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, beiwang.name, data.card.name)
  end,
})

beiwang:addAcquireEffect(function (self, player, is_start)
  if not is_start then
    local room = player.room
    room.logic:getEventsByRule(GameEvent.UseCard, 1, function(e)
      local use = e.data
      if use.card.type == Card.TypeBasic or use.card:isCommonTrick() then
        room:setPlayerMark(player, beiwang.name, use.card.name)
        return true
      end
    end, 0)
  end
end)

return beiwang
